﻿using IMES.Common.Domain.SirModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace GlennQuigmire.API.Application.Commands
{
    public class ProductSearchHander : IRequestHandler<ProductSearchCommand, ResultPage<Product>>
    {

        private readonly IBaseRepositorys<Product> productRepository;


        public ProductSearchHander(IBaseRepositorys<Product> productRepository)
        {
            this.productRepository = productRepository;
        }


        /// <summary>
        /// 产品信息查询显示
        /// </summary>
        /// <param name="request">产品信息查询显示请求对象</param>
        /// <param name="cancellationToken">允许取消异步任务</param>
        /// <returns>产品信息查询显示响应对象</returns>
        public Task<ResultPage<Product>> Handle(ProductSearchCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new ResultPage<Product>();
                var list = productRepository.GetAll().Where(c => !c.DeleteDate);
                if (!string.IsNullOrEmpty(request.ProductNumber))
                {
                    list = list.Where(c => c.ProductNumber.Contains(request.ProductNumber));
                }
                if (!string.IsNullOrEmpty(request.ProductName))
                {
                    list = list.Where(c => c.ProductName.Contains(request.ProductName));
                }
                if (!string.IsNullOrEmpty(request.ProductSpecification))
                {
                    list = list.Where(c => c.ProductSpecification.Contains(request.ProductSpecification));
                }
                if (request.ProcessRouteId!=0)
                {
                    list = list.Where(c => c.ProcessRouteId == request.ProcessRouteId);
                }

                if (request.ProductStats != 0)
                {
                    list = list.Where(c => c.ProductStats == request.ProductStats);
                }
                if (request.MaxInventory != 0)
                {
                    list = list.Where(c => c.MaxInventory <= request.MaxInventory);
                }
                if (request.MinInventory != 0)
                {
                    list = list.Where(c => c.MinInventory >= request.MinInventory); 
                }
                if (request.SaveInventory != 0)
                {
                    list = list.Where(c => c.SaveInventory == request.SaveInventory);
                }
                if (request.InventoryStore != 0)
                {
                    list = list.Where(c => c.InventoryStore == request.InventoryStore);
                }
                if (!string.IsNullOrEmpty(request.CreateStartTime))
                {
                    list = list.Where(c => c.CreateDate >= Convert.ToDateTime((request.CreateStartTime)));
                }
                if (!string.IsNullOrEmpty(request.CreateEndTime))
                {
                    list = list.Where(c => c.CreateDate <= Convert.ToDateTime((request.CreateEndTime)));
                }
                if (!string.IsNullOrEmpty(request.UpdateStartTime))
                {
                    list = list.Where(c => c.UpdateDate >= Convert.ToDateTime((request.UpdateStartTime)));
                }
                if (!string.IsNullOrEmpty(request.UpdateEndTime))
                {
                    list = list.Where(c => c.UpdateDate <= Convert.ToDateTime((request.UpdateEndTime)));
                }

                result.totalCount = list.Count();
                result.totalPage = (int)Math.Ceiling((double)result.totalCount / request.PageSize);
                result.Data = list.OrderBy(c=>c.ProductId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                return Task.FromResult(result);


            }
            catch (Exception ex)
            {

                throw;
            }
        }
    }
}
